<?php 
 include_once('config/config.php');
 
 if($_POST)
 {
   $action = $_POST['action'];
 
   if($action == 'del')
   {
     $id = $_POST['id'];
 
     $admin = find("SELECT * FROM `pre_person` WHERE `id` = '$id'");
 
     if(!$admin)
     {
       error('该用户无法删除');
     }
 
     // 用户是否是被删除 没有登录功能
 
     $result = delete('person',"`id` = '$id'");
 
     if($result)
     {
       @is_file($admin['avatar']) && @unlink($admin['avatar']);
       success('删除成功');
     }else{
       error('删除失败');
     }
     
   }
 }
 
 // 获取当前的页码
 $page = isset($_GET['page']) ? $_GET['page'] : 1;
 
 // 获取admin表的数据总数
 $CountSql = "SELECT COUNT(*) AS num FROM pre_person";
 $count = find($CountSql);
 
 // 每页显示多少条数据
 $limit = 2;
 
 // 每页显示多少个分页的页码数
 $size = 5;
 
 $ShowPage = page($page,$count['num'],$limit,$size);
 
 // 偏移量 -> 索引值
 $start = ($page - 1) * $limit;
 
 $sql = "SELECT pre_person.*, pre_job.name as jobname, pre_department.name as depname FROM pre_person LEFT JOIN pre_job ON pre_person.jobid = pre_job.id LEFT JOIN pre_department ON pre_person.depid = pre_department.id LIMIT $start , $limit";
 // 查询所有数据
 $result = all($sql);
 
// 序号
$num = 0;

 function setsex($val){
	 if($val == 1){
		 return '男';
	 }elseif($val == 2){
		 return '女';
	 }else{
		 return '未知';
	 }
 }
 
?>
<!DOCTYPE html>
<html lang="en">
  <head>
    <!-- 引入公共样式 -->
	<?php include_once('common/meta.php'); ?>
    
  </head>

  <body> 
    
    <!-- 头部 -->
    <?php include_once('common/header.php'); ?>
	
   <!-- 侧边菜单 -->
   <?php include_once('common/sidebar.php'); ?>

    <div class="content">
        <div class="header">
            <h1 class="page-title">用户列表</h1>
        </div>
        <ul class="breadcrumb">
            <li><a href="index.html">Home</a> <span class="divider">/</span></li>
            <li class="active">Person</li>
        </ul>
		
		<div class="container-fluid">
		            <div class="row-fluid">
		                <div class="btn-toolbar">
		                    <button class="btn btn-primary" onclick="location='Personadd.php'"><i class="icon-plus"></i>新增用户</button>
		                </div>
		                <div class="well">
		                    <table class="table">
		                      <thead>
		                        <tr>
		                          <th>ID</th>
								  <th>名字</th>
		                          <th>头像</th>
								  <th>电话</th>
								  <th>性别</th>
								  <th>邮箱</th>
								  <th>国籍</th>
								  <th>部门</th>
								  <th>职位</th>
								  <th>入职时间</th>
		                          <th style="width: 26px;"></th>
		                        </tr>
		                      </thead>
		                      <tbody>
							<?php foreach($result as $item){ ?>
								<tr>
		                          <td><?php echo $item['id']; ?></td>
		                          <td>
									  <?php echo $item['name']; ?>
								  </td>
		                          <td>
								    <img src="<?php echo $item['avatar']; ?>" width="50" />
								  </td>
								  <td><?php echo $item['mobile']; ?></td>
								  <td><?php echo setsex($item['sex']); ?></td>
								  <td><?php echo $item['email']; ?></td>
								  <td><?php echo $item['address']; ?></td>
								  <td><?php echo $item['depname']; ?></td>
								  <td><?php echo $item['jobname']; ?></td>
								  <td><?php echo date("Y-m-d h:i:s",$item['createtime']); ?></td>
		                          <td>
		                              <a href="Personup.php?id=<?php echo $item['id']; ?>"><i class="icon-pencil"></i></a>
		                              <a href="#myModal" class="del" data-id="<?php echo $item['id']; ?>" role="button" data-toggle="modal"><i class="icon-remove"></i></a>
		                          </td>
		                        </tr>    
						    <?php } ?>
								</tbody>
		                    </table>
		                </div>
						
		                <?php echo $ShowPage; ?>
		
		                <div class="modal small hide fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
		                    <div class="modal-header">
		                        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
		                        <h3 id="myModalLabel">Delete Confirmation</h3>
		                    </div>
		                    <div class="modal-body">
		                        <p class="error-text"><i class="icon-warning-sign modal-icon"></i>Are you sure you want to delete the user?</p>
		                    </div>
		                    <div class="modal-footer">
		                        <button class="btn" data-dismiss="modal" aria-hidden="true">Cancel</button>
		                        <button class="btn btn-danger" data-id="" data-dismiss="modal">Delete</button>
		                    </div>
		                </div>
		
		                <div class="container-fluid">
		    <div class="row-fluid">
		        <footer>
		            <hr>
		            <p>© 2017 <a href="#" target="_blank">copyright</a></p>
		        </footer>
		    </div>
		</div>            </div>
		        </div>

        <!-- 底部 -->
		<?php include_once('common/footer.php'); ?>
    </div>
  </body>
</html>
<!-- 引入js文件 -->
<?php include_once('common/script.php'); ?>
<script>
	$('.del').click(function(){
		let id = $(this).data('id');
		$('.btn-danger').attr('data-id',id);
	})
	
	$('.btn-danger').click(function(){
	    let id = $(this).data('id')
	
	    // 发起异步请求
	    $.ajax({
	      // 请求方法 get post 一般都是用post
	      type:'post',
	      // 请求地址
	      url:'index.php',
	      // 请求需要的参数
	      data:{
	        id, //id:id
	        action:'del'
	        // id:5
	      },
	      // 请求返回的数据类型
	      dataType:'json',// html text xml json
	      // 请求成功的回调函数 http 状态码 = 200 
	      success:function(res)
	      {
	        if(res.code === 1)
	        {
	          alert(res.msg)
	          location.reload()
	        }else{
	          alert(res.msg)
	        }
	      },
	      // 请求失败的回调函数 404 301 500等
	      error:function(error)
	      {
	        console.log(error)
	
	      }
	
	    })
	
	  })
</script>